import storageSession from "redux-persist/lib/storage/session";
import {persistReducer} from "redux-persist";

import {CHECK_REGISTER} from "./constants";

const persistConfig = {
  key: "user",
  storage: storageSession,
  whitelist: ["isUser", "info"] //  白名单:isUser会存入缓存，其他不会存
};

const defaultState = {
  registered: true
};

function reducer(state = defaultState, action) {
  switch (action.type) {
    case CHECK_REGISTER:
      return {...state, registered: action.registered};
    default:
      return state;
  }
}

//  将persistConfig中的whitelist存储到缓存中(刷新浏览器redux中的此值不会重置),不在白名单里的则正常存储(刷新浏览器redux数据重置)
export default persistReducer(persistConfig, reducer);

